package se.webbzon.boltzmann.game;

import se.webbzon.boltzmann.battle.environment.BattleEnvironment;
import se.webbzon.boltzmann.battle.environment.BattleInflatorGroup;
import se.webbzon.boltzmann.battle.factory.property.DefaultBPFGroup;
import se.webbzon.boltzmann.game.environment.GameEnvironment;
import se.webbzon.boltzmann.game.environment.GameInflatorGroup;
import se.webbzon.boltzmann.player.Player;

public abstract class Game {
	
	// The name of this game
	private final String name;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new game with the given name. **/
	public Game(String name) {
		this.name = name;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns true if this game has a backup. **/
	public abstract boolean hasBackup();
	
	/** Returns the backup game for this game. **/
	public abstract Game getBackup();
	
	/** Loads the current state of the game from the underlying
	 * storage system. **/
	public abstract GameEnvironment load(GameConfiguration config) throws Exception;
	
	/** Saves the current state of the game to the underlying 
	 * storage system. **/
	public abstract void save(GameEnvironment env) throws Exception;
	
	/** Returns the name of the game. **/
	public String getName() {
		return name;
	}
	
	/*============================================================
	Protected Methods
	============================================================*/
	
	/** Creates a new game environment for a new game. **/
	public GameEnvironment createGame(GameConfiguration config) throws Exception {
		BattleEnvironment benv = new BattleEnvironment(new BattleInflatorGroup());
		GameEnvironment genv = new GameEnvironment(this, benv, new GameInflatorGroup(new DefaultBPFGroup()));
		for (int p = 0; p < config.players(); p++)
			genv.addPlayer(new Player(config.getPlayer(p)));
		return genv;
	}

}
